﻿using DevExpress.Web.ASPxGridView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFatura
{
    public partial class Liste : System.Web.UI.Page
    {
        Mikro mk = new Mikro();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userno"] == null)
            {
                Response.Redirect("/Default.aspx");
            }

            if (Request.QueryString.Count > 0)
            {
                string islem = Request.QueryString["tip"];
                if (islem == "fatura")
                {
                    ASPxGridView1.DataSource = mk.FaturaListe(((Cari)Session["cari"]).Kodu,Session["seri"].ToString());
                    GridViewDataTextColumn col1 = new GridViewDataTextColumn();
                    col1.Caption = "Tarih";
                    col1.FieldName = "Tarih";
                    col1.Width = 200;

                    GridViewDataTextColumn col2 = new GridViewDataTextColumn();
                    col2.Caption = "Evrak Seri";
                    col2.FieldName = "Seri";
                    col2.Width = 100;

                    GridViewDataTextColumn col3 = new GridViewDataTextColumn();
                    col3.Caption = "Evrak Sıra";
                    col3.FieldName = "Sira";
                    col3.Width = 100;

                    GridViewDataTextColumn col4 = new GridViewDataTextColumn();
                    col4.Caption = "Tutar";
                    col4.FieldName = "Meblag";
                    col4.Width = 200;

                    ASPxGridView1.Columns.Clear();
                    ASPxGridView1.Columns.Add(col1);
                    ASPxGridView1.Columns.Add(col2);
                    ASPxGridView1.Columns.Add(col3);
                    ASPxGridView1.Columns.Add(col4);
                }
                else
                {
                    ASPxGridView1.DataSource = mk.CariListe();
                    GridViewDataTextColumn col1 = new GridViewDataTextColumn();
                    col1.Caption = "Cari Kodu";
                    col1.FieldName = "Kodu";
                    col1.Width = 200;

                    GridViewDataTextColumn col2 = new GridViewDataTextColumn();
                    col2.Caption = "Cari Ünvanı";
                    col2.FieldName = "Unvani";
                    col2.Width = 470;

                    ASPxGridView1.Columns.Clear();
                    ASPxGridView1.Columns.Add(col1);
                    ASPxGridView1.Columns.Add(col2);
                }
            }

            ASPxGridView1.DataBind();
            ASPxGridView1.KeyFieldName = "ID";
            ASPxGridView1.Settings.ShowVerticalScrollBar = true;
            ASPxGridView1.Settings.ShowHorizontalScrollBar = true;

        }
        protected void btnOnay_Click(object sender, EventArgs e)
        {
            int row = -1;
            for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
            {
                if (ASPxGridView1.Selection.IsRowSelected(i))
                {
                    row = i;
                    break;
                }
            }

            var sonuc = ASPxGridView1.GetRow(row);
            if (sonuc != null)
            {
                if (Request.QueryString.Count > 0)
                {
                    if (Request.QueryString["tip"] == "fatura")
                    {
                        EvrakOzet evr = sonuc as EvrakOzet;
                        Response.Redirect("/Fatura.aspx?seri=" + evr.Seri + "&sira=" + evr.Sira);
                    }
                    else
                    {
                        Session["cari"] = sonuc;
                        Response.Redirect("/Fatura.aspx");
                    }
                }
            }
        }
        protected void btnMenu_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Menu.aspx");
        }
    }
}